package dse.messages.singleversion;

import dse.Experiment;
import dse.ExperimentRecorder;
import dse.messages.Message;
import dse.messages.MessageHandler;
import dse.messages.MessageSender;

public class HasShareResponseMessageHandler 
extends MessageSender 
implements MessageHandler
{
	private boolean affermative;
	private HasShareRequester hasShareRequester;
	
	
	
	public HasShareResponseMessageHandler (
			Experiment experiment, 
			boolean affermative,
			HasShareRequester hasShareRequester
			)
	{
		super(experiment);
		
		this.affermative = affermative;
		this.hasShareRequester = hasShareRequester;
	}

	
	
	public boolean acceptMessage (
			Message message
			)
	{
		return true;
	}
	
	public void messageReceived (
			ExperimentRecorder recorder, 
			Message message,
			double timeLeft
			)
	{
		recorder.debug(this, "msgRcvd: " + message, timeLeft);
		// OK, now the hasShareRequester object has to decide what to do...
		if (affermative) {
			recorder.shareLookupSuccessful();
			hasShareRequester.shareFound(recorder, timeLeft);
		} else {
			recorder.shareLookupFailed();
			hasShareRequester.shareNotFound(recorder, timeLeft);
		}
	}


	public void messageInterupted (
			ExperimentRecorder recorder,
			Message message, 
			double timeLeft
			)
	{
		recorder.debug(this, "msgIntrpt: " + message, timeLeft);
		recorder.shareLookupFailed();
		hasShareRequester.shareNotFound(recorder, timeLeft);
	}

}
